package fr.cephb.locustree.loaders;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.regex.Pattern;

import fr.cephb.io.IOUtils;
import fr.cephb.locustree.LTStreamLoader;

/**
 * 
 * AbstractLTStreamLoader
 *
 */
public abstract class AbstractLTStreamLoader
	extends AbstractLTLoader
	implements LTStreamLoader
	{
	
	
	private BufferedReader in=null;
	protected final static Pattern TAB=Pattern.compile("[\t]"); 
	

	
	protected AbstractLTStreamLoader()
		{
		
		}
	
	@Override
	public void open(String uri) throws IOException
		{
		this.in= new BufferedReader(new InputStreamReader(
				IOUtils.openInputStream(uri)));

		}
	
	@Override
	public void close() throws IOException
		{
		if(in!=null) in.close();
		in=null;
		}

	
	
	
	
	
	
	@Override
	public boolean next() throws IOException
		{
		if(in==null) return false;
		this.chromosome=null;
		this.mappedObject=null;
		this.keywords.clear();
		while(true)
			{
			String line=in.readLine();
			++this.nLine;

			time(line);

			if(line==null)
				{
				close();
				return false;
				}
			if(ignoreLine(line)) continue;
			if(make(TAB.split(line)))
				{
				this.inserted++;
				return true;
				}
			}
		}

	protected abstract boolean make(String tokens[]);
	
	protected boolean ignoreLine(String s)
		{
		return this.nLine==1L;
		}
	
	
	
	}
